<template>
  <!-- 外层容器 -->
  <div class="dong-dropdown">
    <Dong-Tooltip :trigger="trigger" :placement="placement" :open-delay="openDelay" :close-delay="closeDelay"
      :manual="manual" @visible-change="visibleChangeHandler" ref="tooltipRef">
      <slot />
      <template #content>
        <ul class="dong-dropdown__menu">
          <template v-for="item in menuOptions" :key="item.key">
            <!-- 分割线 -->
            <li v-if="item.divided" class="divided-placeholder"></li>
            <li class="dong-dropdown__item" :class="{ 'is-disabled': item.disabled }" :id="`dropdown-item-${item.key}`"
              @click="itemClick(item)">
              <RenderVNode :v-node="item.label" />
              <!-- {{ item.label }} -->
            </li>
          </template>
        </ul>
      </template>
    </Dong-Tooltip>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import propsObj from './props'
import RenderVNode from '../Common/RenderVNode'
defineOptions({
  name: 'DongDropdown'
})
const emit = defineEmits(['select', 'visible-change'])
const props = defineProps(propsObj)
const tooltipRef = ref(null)

const itemClick = (item) => {
  if (item.disabled) return
  emit('select', item)
  // 之后我们再这里添加一个判断
  // 如果用户传递了 hideAfterClick 属性
  // 说明需要点击后关闭下拉菜单
  // 这里我们就手动的调用 hide 方法关闭即可
  if (props.hideAfterClick) tooltipRef.value.hide()
}
const visibleChangeHandler = (e) => {
  emit('visible-change', e)
}
defineExpose({
  show: () => {
    tooltipRef.value.show()
  },
  hide: () => {
    tooltipRef.value.hide()
  }
})
</script>

<style lang="scss" scoped>
.bar {
  color: orange;
  font-style: italic;
}
</style>
